
package com.caration.shell.preference;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

import com.caration.shell.Home;
import com.caration.shell.R;
import com.caration.shell.VersionDetailActivity;
import com.caration.shell.config.CommonConfig;
import com.caration.shell.cr3d.SharedData;

public class SettingPreferenceActivity extends PreferenceActivity implements
        OnPreferenceClickListener, OnPreferenceChangeListener {

    private SeekBarPreference mPageMenuBtnAlphaPref;
    private SeekBarPreference mSensitivityPref;
    private int mScreenOrientation;
    private int mDirection;
    private boolean mNotify;

    public static void startup() {
        Intent i = new Intent();
        i.setClass(SharedData.getContext(), SettingPreferenceActivity.class);
        Home.getInstance().startActivity(i);
    }

    private String getVersion() {
        String version = getString(R.string.mn_version);
        try {
            String str = version + ":"
                    + getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            version = str;
            return version;
        } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
            while (true)
                localNameNotFoundException.printStackTrace();
        }
    }

    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        addPreferencesFromResource(R.xml.preferences);

        findPreference("emailPref").setOnPreferenceClickListener(this);
        findPreference("restartShell").setOnPreferenceClickListener(this);
        findPreference("unread").setOnPreferenceClickListener(this);
        findPreference(CommonConfig.KEY_VERSION).setTitle(getVersion());
        findPreference("version_detail").setTitle(
                getVersion() + " " + getString(R.string.mn_version_detail));
        findPreference("version_detail").setSummary(
                getString(R.string.mn_version_detail_summary) + " 2012/10/05");
        findPreference("version_detail").setOnPreferenceClickListener(this);
        findPreference(CommonConfig.KEY_SCREEN_ORIENT).setOnPreferenceClickListener(this);
        findPreference("db_export").setOnPreferenceClickListener(this);
        findPreference("db_import").setOnPreferenceClickListener(this);
        findPreference("db_reset").setOnPreferenceClickListener(this);
        findPreference("video").setTitle("V1.7.0 " + getString(R.string.mn_introduction_video));
        findPreference("video").setOnPreferenceClickListener(this);
        findPreference("drawer").setOnPreferenceClickListener(this);
        findPreference("gesture").setOnPreferenceClickListener(this);
        findPreference(CommonConfig.KEY_PREF_DIRECTION).setOnPreferenceClickListener(this);

        mNotify = CommonConfig.isShowNotification();
        mScreenOrientation = CommonConfig.getScreenOrientation();
        mDirection = CommonConfig.getPrefDirection();

        LogoPreference logo=(LogoPreference)findPreference("logo");
        logo.setLogo(getResources().getDrawable(R.drawable.ic_crshell));
        
        mSensitivityPref = (SeekBarPreference) findPreference(CommonConfig.KEY_SLIDING_SENSI);
        mSensitivityPref.setMaxSeek(4);
        mSensitivityPref.setMinSeek(0);
        mSensitivityPref.seek(CommonConfig.getSlidingSensitivity());

        SeekBarPreference skbarPref = (SeekBarPreference) findPreference(CommonConfig.KEY_WALLPAPER_TRANS_SPEED);
        skbarPref.setMaxSeek(5);
        skbarPref.setMinSeek(0);

        mPageMenuBtnAlphaPref = ((SeekBarPreference) findPreference(CommonConfig.KEY_PAGE_MENU_BUTTON_ALPHA));
        mPageMenuBtnAlphaPref.setMaxSeek(100);
        mPageMenuBtnAlphaPref.setMinSeek(0);
        mPageMenuBtnAlphaPref.setUnitsRight("%");
        mPageMenuBtnAlphaPref.seek(CommonConfig.getPageMenuButtonAlpha());

        CheckBoxPreference localCheckBoxPreference1 = (CheckBoxPreference) findPreference(CommonConfig.KEY_STANDARD_SLIDING_MODE);
        CheckBoxPreference localCheckBoxPreference2 = (CheckBoxPreference) findPreference(CommonConfig.KEY_FAST_SLIDING_MODE);
        localCheckBoxPreference1.setChecked(CommonConfig.isStandardSlidingMode());
        localCheckBoxPreference2.setChecked(CommonConfig.isFastSlidingMode());
        localCheckBoxPreference1.setOnPreferenceChangeListener(this);
        localCheckBoxPreference2.setOnPreferenceChangeListener(this);

        localCheckBoxPreference1 = (CheckBoxPreference) findPreference(CommonConfig.KEY_PAGE_MENU_BUTTON_STATE);
        localCheckBoxPreference1.setChecked(CommonConfig.isPageMenuButtonState());
        localCheckBoxPreference1.setOnPreferenceChangeListener(this);
        localCheckBoxPreference1 = (CheckBoxPreference) findPreference(CommonConfig.KEY_INDICATOR_MODE);
        localCheckBoxPreference1.setChecked(CommonConfig.isIndicatorModeOn());
        localCheckBoxPreference1.setOnPreferenceChangeListener(this);
        localCheckBoxPreference1 = (CheckBoxPreference) findPreference(CommonConfig.KEY_VIBRATION_FEEDBACK);
        localCheckBoxPreference1.setChecked(CommonConfig.isVibrationFeedbackOn());
        localCheckBoxPreference1.setOnPreferenceChangeListener(this);

        CheckBoxPreference localCheckBoxPreference5 = (CheckBoxPreference) findPreference(CommonConfig.KEY_DESKTOP_LOCK);
        localCheckBoxPreference5.setChecked(CommonConfig.isDesktopLock());
        localCheckBoxPreference1 = (CheckBoxPreference) findPreference(CommonConfig.KEY_AUTO_HIDE_SIDE_BAR);
        localCheckBoxPreference1.setChecked(CommonConfig.isAutoHideSideBar());
        CheckBoxPreference localCheckBoxPreference3 = (CheckBoxPreference) findPreference(CommonConfig.KEY_SCREEN_INFINITE_SCROLL);
        localCheckBoxPreference3.setChecked(CommonConfig.isScreenInfiniteScroll());
        localCheckBoxPreference2 = (CheckBoxPreference) findPreference(CommonConfig.KEY_SLIDING_DOCK);
        localCheckBoxPreference2.setChecked(CommonConfig.isSlidingDock());
        CheckBoxPreference localCheckBoxPreference4 = (CheckBoxPreference) findPreference(CommonConfig.KEY_SHOW_NOTIFICATION);
        localCheckBoxPreference4.setChecked(mNotify);
        localCheckBoxPreference3.setOnPreferenceChangeListener(this);
        localCheckBoxPreference5.setOnPreferenceChangeListener(this);
        localCheckBoxPreference4.setOnPreferenceChangeListener(this);
        localCheckBoxPreference2.setOnPreferenceChangeListener(this);
        localCheckBoxPreference1.setOnPreferenceChangeListener(this);

        SettingFolderPreference.init(this);
        SettingWallpaperPreference.init(this);
        SettingScreenOrientPreference.init(findPreference(CommonConfig.KEY_SCREEN_ORIENT));
        SettingDirectPreference.init(findPreference(CommonConfig.KEY_PREF_DIRECTION));
    }

    public void onDestroy() {
        SettingFolderPreference.onDestory();
        SettingWallpaperPreference.onDestroy();
        super.onDestroy();
    }

    public void onPause() {
        SettingWallpaperPreference.update();
        if (mSensitivityPref != null)
            CommonConfig.setSlidingSensitivity(mSensitivityPref.getProgress());
        if (mPageMenuBtnAlphaPref != null)
            CommonConfig.setPageMenuButtonAlpha(mPageMenuBtnAlphaPref.getProgress());
        if (CommonConfig.getScreenOrientation() == mScreenOrientation) {
            if (CommonConfig.getPrefDirection() != mDirection)
                // aw.a(ShellConfig.getPrefDirection());
                if (CommonConfig.isShowNotification() != mNotify) {
                    // com.tsf.shell.action.q.a(!mNotify);
                }
        } else {
            // Home.getInstance().b();TODO:
        }
        super.onPause();
    }

    public boolean onPreferenceClick(Preference pref) {
        String key = pref.getKey();
        if ("emailPref".equals(key)) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            String addr = getString(R.string.mn_feedback_summary);
            intent.putExtra(Intent.EXTRA_EMAIL, addr);
            intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + ":"
                    + getString(R.string.mn_feedback));
            intent.setType("text/plain");
            startActivity(Intent.createChooser(intent, "Email"));
        } else if ("restartShell".equals(key)) {
            Home.getInstance().restart();
        } else if (CommonConfig.KEY_SCREEN_ORIENT.equals(key)) {
            SettingScreenOrientPreference.show(this, findPreference(CommonConfig.KEY_SCREEN_ORIENT));
        } else if (CommonConfig.KEY_PREF_DIRECTION.equals(key)) {
            SettingDirectPreference.show(this, findPreference(CommonConfig.KEY_PREF_DIRECTION));
        } else if ("db_export".equals(key)) {
            // com.tsf.shell.a.a.b(this);
        } else if ("db_import".equals(key)) {
            // com.tsf.shell.a.a.a(this);
        } else if ("db_reset".equals(key)) {
            // com.tsf.shell.a.a.c(this);
        } else if ("video".equals(key)) {
            String[] strs = new String[2];
            strs[0] = "YOUTUBE";
            strs[1] = "YOUKU优酷";
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setItems(strs, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String youku = "http://v.youku.com/v_show/id_XNDUwODE2MzI0.html";
                    String youtube = "http://youtu.be/ia0uaql99Kc";
                    Uri uri = Uri.parse(which == 1 ? youku : youtube);
                    startActivity(new Intent(Intent.ACTION_VIEW, uri));
                }
            });
            builder.create().show();
        } else if ("version_detail".equals(key)) {
            VersionDetailActivity.startup();
        } else if ("drawer".equals(key)) {
            SettingDrawerPreferenceActivity.startup();
        } else if ("gesture".equals(key)) {
            SettingGesturePreferenceActivity.startup();
        } else if ("unread".equals(key)) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.tsf.shell.services.notifier",
                    "com.tsf.shell.services.notifier.MainActivity"));
            intent.setAction(Intent.ACTION_VIEW);
            try {
                startActivity(intent);
            } catch (Exception _ex) {
                // ac ac1 = new ac(this);
                // ap.a(f, 0x7f0a00ed, ac1);
            }
        }
        return true;
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        boolean flag = ((Boolean) newValue).booleanValue();
        String key = preference.getKey();
        if (key.equals(CommonConfig.KEY_STANDARD_SLIDING_MODE)) {
            CommonConfig.setStandardSlidingMode(flag);
            // fm.h().j().a(flag);
        } else if (key.equals(CommonConfig.KEY_FAST_SLIDING_MODE)) {
            CommonConfig.setFastSlidingMode(flag);
            // fm.h().j().b(flag);
        } else if (key.equals(CommonConfig.KEY_SCREEN_INFINITE_SCROLL)) {
            CommonConfig.setInfiniteScroll(flag);
            // fm.h().d();
        } else if (key.equals(CommonConfig.KEY_DESKTOP_LOCK)) {
            CommonConfig.setDesktopLock(flag);
        } else if (key.equals(CommonConfig.KEY_SLIDING_DOCK)) {
            CommonConfig.setSlidingDock(flag);
        } else if (key.equals(CommonConfig.KEY_SHOW_NOTIFICATION)) {
            mNotify = flag;
        } else if (key.equals(CommonConfig.KEY_AUTO_HIDE_SIDE_BAR)) {
            CommonConfig.setAutoHideSideBar(flag);
            // if (flag)
            // fm.k().c(false);
            // else
            // fm.k().b(false);
        } else if (key.equals(CommonConfig.KEY_PAGE_MENU_BUTTON_STATE)) {
            CommonConfig.setPageMenuButtonState(flag);
            // fm.h().e().a(flag);
        } else if (key.equals(CommonConfig.KEY_INDICATOR_MODE)) {
            CommonConfig.setIndicatorMode(flag);
        } else if (key.equals(CommonConfig.KEY_VIBRATION_FEEDBACK)) {
            CommonConfig.setVibrationFeedback(flag);
        }

        return true;
    }
}
